package com.xyht.sca_s.student_manage_system.common.config;

import com.xyht.sca_s.student_manage_system.auth.config.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册TestInterceptor拦截器
        InterceptorRegistration registration = registry.addInterceptor(myInterceptor());
        registration.addPathPatterns("/**");                      //拦截当前路径
        registration.excludePathPatterns(                         //添加不拦截路径
//                "/**",                     //TODO 不拦截所有路径
                "/",    //放行  localhost:8080/xyht/路径
                "/wx/mp/receive",    //放行
                "/wx/mp/bindMa",    //放行
                "/**/schoolEditor/all",
                "/**/user/webLogin",            //登录
                "/**/user/wxUserLogin",            //登录
                "/**/user/wxLogin",            //登录
                "/**/user/password/verifyCode*",
                "/**/file/download",
                "/**/file/userPicUpload",  //用户头像上传
                "/static/**",
                "/**/*.html",            //html静态资源
                "/**/*.js",              //js静态资源
                "/**/*.css",             //css静态资源
                "/**/*.woff",
                "/**/*.ttf",
                "/**/*.png",
                "/**/visitor/smsVisitorUser/smsVisitorUserAdd",
                "/**/visitor/smsVisitorOperatingManual/selectSmsVisitorOperatingManual",
                "/**/visitor/smsVisitorNeedKnow/selectSmsVisitorNeedKnow",
                "/**/visitor/smsVisitorUser/smsVisitorUserRegister",
                "/**/visitor/smsVisitorRegister/select",
                "/**/patrol/smsPatrol/scanQr",
                "/**/signUp/smsSignUpSystem/selectSmsSignUpOrgSystem",
                "/**/signUp/smsSignUpSystem/smsSignUpSystemAdd"

//                "/**/visitorRegister/smsVisitorRegister/**",
//                "/**/visitor/smsVisitorRegister/visitor/smsVisitorRegisterAdd",
//                "/**/patrolImage/smsPatrolImage/**",
//                "/**/visitor/smsVisitorUser/**",
//                "/**/visitor/smsVisitorRegister/visitor/selectSmsVisitorRegister",
//                "/**/visitor/smsVisitorRegister/selectById"

        );
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
//                .allowedHeaders("Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token,Access-Control-Allow-Headers")
                .maxAge(3600);
    }
}
